<template>
  <div class="app-container">
    <el-form ref="from" label-width="120px" :model="teacher">
      <el-form-item label="讲师名称" >
        <el-input v-model="teacher.name" style="width: 150px"/>
      </el-form-item>

      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort"  :min="0"></el-input-number>
      </el-form-item>

      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level" placeholder="请选择">
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="首席讲师" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career" />
      </el-form-item>

      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro" :rows="10" type="textarea" />
      </el-form-item>
      <!-- 讲师头像 -->
      <el-form-item label="讲师头像">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="teacher.avatar|| ''"/>
        <!-- 文件上传按钮 -->
        <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像</el-button>

        <!--
          v-show：是否显示上传组件
          :key：类似于id，如果一个页面多个图片上传控件，可以做区分
          :url：后台上传的url地址
          @close：关闭上传组件
          @crop-upload-success：上传成功后的回调
            <input type="file" name="file"/>
        -->
        <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API+'/eduOss/fileOss/uploadFile'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdateTeacher">保存</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
  import teacher from "@/api/edu/teacher";
  import ImageCropper from '@/components/ImageCropper'
  import PanThumb from '@/components/PanThumb'


  export default {
    name: "save",
    data() {
      return {
        teacher : {
          name: "",
          sort: 0,
          level: null,
          career: "",
          intro: "",
          avatar: "http"
        },
        saveBtnDisabled: false,// 保存按钮是否 不显示
        // 是否显示上传按钮
        imagecropperShow: false,
        // 文件上传组件的唯一标识
        imagecropperKey: 0,
        // 获取dev.env.js中的接口地址
        BASE_API: process.env.BASE_API,

      }
    },
    components: {
      ImageCropper,
      PanThumb
    },
    created() {
      this.init()
    },
    watch: {
      $route(to, from) {
        this.init();
      }
    },

    methods:{

      // 关闭上传弹框的方法
      close() {
        this.imagecropperShow = false
        // 初始化组件
        this.imagecropperKey ++
      },
      // 上传成功的方法
      cropSuccess(data) {
        this.imagecropperShow = false
        this.teacher.avatar = data.url
        this.imagecropperKey ++
      },

      init(){
        if (this.$route.params && this.$route.params.id) {
          const id = this.$route.params.id;
          this.getTeacher(id);
        } else {
          this.teacher = { };
        }
      },
      saveOrUpdateTeacher(){
        if (!this.teacher.id) {
          // 添加
          this.saveTeacher();
        } else {
          // 修改
          this.updateTeacher();
        }
      },

      saveTeacher(){
        teacher.addTeacher(this.teacher)
          .then(response => {
            if (response.code == 20000) {
              this.$message({
                type: "success",
                message: "添加成功!"
              });

              this.$router.push({ path: "/teacher/table" });
            } else {
              this.$message.error("添加失败!");
            }
          })
          .catch(error => {
             console.log(error)
          })
      },

      updateTeacher(){
        teacher.updateTeacherInfo(this.teacher.id, this.teacher)
          .then(res => {
            if (res.code == 20000) {
              this.$message({
                type: "success",
                message: "修改成功!"
              });
            } else {
              this.$message.error("修改失败!");
            }
            this.$router.push({ path: "/teacher/table" });
          })
          .catch(error => {
            console.log(error)
          })
      },

      getTeacher(id) {
        teacher.getTeacherInfo(id)
        .then(res => {
          this.teacher = res.data.teacher
        })
      },

    }
  }
</script>

<style scoped>

</style>
